package com.swn.cm.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.swn.cm.service.TestProjectService;
import com.swn.common.util.JSONUtil;
import com.swn.common.util.PageListData;

@Controller
@RequestMapping("/test")
public class TestProjectController {
	
	//private final static String UPLOADDIR= "/image";
	
	@Autowired
	private TestProjectService testService;
	
		
	@RequestMapping("uploadImage")
	@ResponseBody
	public Map<String, Object> uploadImage(MultipartFile file,
			HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		// 文件上传路径
		String fileName = file.getOriginalFilename();
		//String path = request.getSession(false).getServletContext().getRealPath("/uploadFile")
		//		+ "\\" + fileName;
		String path = "F:\\space\\common_interface\\WebContent\\uploadFile"+"\\"+fileName;
		System.out.println(path);
		// 写入路径
		// 返回固定格式{"code": 0,"msg": "","data": {"src":
		// "http://cdn.layui.com/123.jpg"}}
		FileOutputStream outPutStream = null;
		try {
			/*file.transferTo(new File(path));*/
			File newFile = new File(path);
			outPutStream = new FileOutputStream(newFile);
			InputStream inputStream = file.getInputStream();
			byte[] buffer = new byte[1024];
			int bytesRead = -1;
			while ((bytesRead = inputStream.read(buffer)) != -1) {
				outPutStream.write(buffer, 0, bytesRead);
			}
			outPutStream.flush();
			resultMap.put("path", path);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				outPutStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return resultMap;
	}
	
	    
	    @RequestMapping("addTestProject")
		@ResponseBody
		public String addTestProject(String signupForm) throws Exception{
			
			@SuppressWarnings("unchecked")
			Map<String, Object> param = JSONUtil.getMapFromJsonString(signupForm);
			int count = testService.addTestProject(param);
			if(count>0){
				return "success";
			}
			return "error";
		}
	
	
	@RequestMapping("queryProjectList")
	@ResponseBody
	public Map<String, Object> queryProjectList(Integer start,Integer length,String signupForm){
		Map<String, Object> map = new HashMap<String, Object>();
		PageListData pageListData = new PageListData();
		try {
			pageListData.setStart(start);
			pageListData.setLength(length);
			@SuppressWarnings("unchecked")
			Map<String, Object> param = JSONUtil.getMapFromJsonString(signupForm);
			List<Map<String, Object>> projectList = testService.queryProjectList(pageListData,param);
			int total = pageListData.getCount();
			map.put("recordsTotal", total);                  // 总记录数
			map.put("recordsFiltered", total);               // 过滤后的总记录数
			map.put("data", projectList);   
		} catch (Exception e) {
			logger.info("查询保存了");
		}
		return map;
		
	}

	
	@RequestMapping("updateTestProject")
	@ResponseBody
	public String updateTestProject(String signupForm, long project_id){
		@SuppressWarnings("unchecked")
		Map<String, Object> param = JSONUtil.getMapFromJsonString(signupForm);
		param.put("project_id", project_id);
		if(testService.updateTestProject(param)==1){
			return "success";
		}
		return "error";
		
	}
	
	
	@RequestMapping("queryProjectById")
	@ResponseBody
	public Map<String, Object> queryProjectById(long project_id) throws Exception{
		Map<String, Object>  map = testService.queryProjectById(project_id);
		return map;
	}
	
	@RequestMapping("deleteTestProject")
	@ResponseBody
	public String deleteTestProject(long project_id){
		if(testService.deleteTestProject(project_id)>=0){
			return "success";
		}
		return "error";
	}
	
	@RequestMapping("queryProjectTypes")
	@ResponseBody
	public List<Map<String, Object>> queryProjectTypes(){
		List<Map<String, Object>> projectTypesList = testService.queryProjectTypes();
		return projectTypesList;
	}
	
	Logger logger = Logger.getLogger(TestProjectController.class);
	
}
